﻿namespace FFF.Test.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Globalization;
    using System.IdentityModel.Tokens.Jwt;
    using System.Linq;
    using System.Security.Claims;
    using System.Text;
    using System.Threading.Tasks;
    using Abp.Localization;
    using Abp.Runtime.Security;
    using Microsoft.AspNetCore.Authorization;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.SignalR;
    using Microsoft.Extensions.Configuration;
    using Microsoft.IdentityModel.Tokens;
    using Senparc.NeuChar.App.Entities;
    using Senparc.Weixin.MP;
    using FFF.Test.Application;
    using FFF.Test.Application.AppServices.Demo;
    using FFF.Test.Application.AppServices.PublicService.Sessions;
    using FFF.Test.Application.AppServices.PublicService.SignalRs;
    using FFF.Test.Controllers.VM;
    using FFF.Test.Util.Helper;
    using FFF.Test.Util.Web;

    /// <summary>
    /// homde
    /// </summary>
    public class HomeController : YdmAbpControllerBase
    {
        private readonly IConfiguration _appConfiguration;
        private readonly ILanguageManager _languageManager;

        /// <summary>
        /// Initializes a new instance of the <see cref="HomeController"/> class.
        /// 初始化
        /// </summary>
        /// <param name="configuration">config对象</param>
        /// <param name="languageManager">语言对象</param>
        public HomeController(
            IConfiguration configuration,
            ILanguageManager languageManager
            )
        {
            this._appConfiguration = configuration;
            this._languageManager = languageManager;
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns>视图的页面</returns>
        public IActionResult Index()
        {
            // return base.Redirect("/Demo/DemoStudentEdit/DemoStudentEditIndex");
            var model = new LanguageSelectionViewModel
            {
                CurrentLanguage = this._languageManager.CurrentLanguage,
                Languages = this._languageManager.GetLanguages(),
                CurrentUrl = this.Request.Path
            };
            return base.View(model);
        }

        /// <summary>
        /// 系统首页
        /// </summary>
        /// <returns>系统首页的</returns>
        public IActionResult SystemIndex()
        {
            //var userInfo = await this._sessionAppservice.GetCurrentUserInfo();
            //var isSuccess = await this._signalAppservice.SendMessage(userInfo.Id, "Hello");

            return base.View();
        }

        /// <summary>
        /// 登录视图
        /// </summary>
        /// <returns>返回登录视图</returns>
        [AllowAnonymous]
        public IActionResult Login()
        {
            return base.View();
        }

        /// <summary>
        /// 修改密码界面
        /// </summary>
        /// <returns>系统首页的</returns>
        public IActionResult ModifyPassword()
        {
            return base.View();
        }

        /// <summary>
        /// 聊天移动端界面
        /// </summary>
        /// <returns>聊天移动端界面view</returns>
        public IActionResult IMMobile()
        {
            return base.View();
        }

        /// <summary>
        /// 微信支付
        /// </summary>
        /// <returns>返回登录视图</returns>
        [AllowAnonymous]
        public IActionResult WxPay()
        {
            return base.View();
        }

        /// <summary>
        /// 注册界面
        /// </summary>
        /// <returns>返回登录视图</returns>
        [AllowAnonymous]
        public IActionResult Register()
        {
            return base.View();
        }

        /// <summary>
        /// 找回密码界面
        /// </summary>
        /// <returns>返回登录视图</returns>
        [AllowAnonymous]
        public IActionResult Forget()
        {
            return base.View();
        }

        /// <summary>
        /// signalr界面
        /// </summary>
        /// <returns>返回登录视图</returns>
        [AllowAnonymous]
        public IActionResult SignalR()
        {
            return base.View();
        }

        /// <summary>
        /// 小程序验证
        /// </summary>
        /// <param name="postModel">小程序验证的model</param>
        /// <param name="echostr">随机字符</param>
        /// <returns>返回登录视图</returns>
        [AllowAnonymous]
        public IActionResult WxOpen(PostModel postModel, string echostr)
        {
            if (CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, "ydm"))
            {
                return base.Content(echostr); //返回随机字符串则表示验证通过
            }
            else
            {
                return base.Content("failed:" + postModel.Signature + "," + Senparc.Weixin.MP.CheckSignature.GetSignature(postModel.Timestamp, postModel.Nonce, "ydm") + "。" +
                    "如果你在浏览器中看到这句话，说明此地址可以被作为微信小程序后台的Url，请注意保持Token一致。");
            }
        }

        /// <summary>
        /// 错误页面
        /// </summary>
        /// <returns>错误页view</returns>
        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return null;

            // return this.View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? this.HttpContext.TraceIdentifier });
        }
    }
}
